/****************************************************************************

	COPYRIGHT(C) MAXSI SOFTWARE, JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010

    This file is part of Maxsi RXP.

    Maxsi RXP is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Maxsi RXP is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Maxsi RXP.  If not, see <http://www.gnu.org/licenses/>.

	*/ #include "MaxsiRXPContributors.h" /*

	If you modify this file, please enter your name below and provide contact
	information in MaxsiRXPContributors.h. For more information please see
	MaxsiRXPContributors.h.
	
	Contributors to this file:

	- Jonas 'Sortie' Termansen
	- [your name here]


	MaxsiRXP.exe
	A system to control MaxsiRXP Systems.
	
	Main.cpp
	Basic WIN32 functions and stuff

****************************************************************************/

#include "MaxsiEngine.h"
#include <io.h>
#include <direct.h>
#include <iostream> 
#include <fcntl.h>
#include <SYS\Stat.h>
#include "Main.h"
#include "UI.h"

int DoAwesomeRXPStuff(HINSTANCE hInstance);

ME_WIN32ENTRYPOINT(MaxsiRXP);

/****************************************************************************

    FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
    PURPOSE: calls initialization function, processes message loop

****************************************************************************/

int WINAPI MaxsiRXP::WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{
	DoAwesomeRXPStuff(hInstance);

	return BaseClass::WinMain(hInstance,hPrevInstance,lpCmdLine,nCmdShow);
}


/****************************************************************************

    FUNCTION: InitApplication(HANDLE)
    PURPOSE: Initializes window data and registers window class

****************************************************************************/

BOOL MaxsiRXP::InitApplication(
    HANDLE hInstance)
{
    WNDCLASS  wc = {0};

    wc.lpfnWndProc = (WNDPROC) MaxsiRXPProc;
    wc.hInstance = (HINSTANCE)hInstance;
    wc.hIcon = LoadIconA((HINSTANCE)hInstance, "ProductIcon");
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
    wc.lpszClassName = TEXT("MaxsiRXP");

	if (!RegisterClass(&wc)) { return FALSE; }

	return TRUE;
}


/****************************************************************************

    FUNCTION:  InitInstance(HANDLE, int)
    PURPOSE:  Saves instance handle and creates main window

****************************************************************************/
BOOL MaxsiRXP::InitInstance(
    HINSTANCE hInstance, 
    INT nCmdShow)
{
    LPTSTR lpszText = NULL;
    BOOL bSuccess = TRUE;

    hInst = hInstance;

    hAccTable = LoadAccelerators(hInst, TEXT("MaxsiRXP"));

	// Get the screen size
	DEVMODE Dev;
	BOOL	Result	=	EnumDisplaySettings(NULL,ENUM_CURRENT_SETTINGS,&Dev);
	int w = 800;
	int h = 600;
	int x = (Result) ? (int)(Dev.dmPelsWidth - w ) / 2 : (int)CW_USEDEFAULT;
	int y = (Result) ? (int)(Dev.dmPelsHeight - h ) / 2 : (int)CW_USEDEFAULT;

	char*	WindowTitle		=	BuildString(1,"MaxsiRXP");

    WindowHWND = CreateWindowA(
        "MaxsiRXP",
        WindowTitle,
        (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX ),
        x, y,
        w, h,
        NULL, NULL, hInstance, NULL );

	delete[] WindowTitle;

	if (!WindowHWND) { return FALSE; }


	ME_Window*	Parent	=	ME_WindowGetPointer(WindowHWND);

	Form = InitializeForm(new MaxsiRXPUI, Parent);

	ShowWindow(WindowHWND, nCmdShow);
    bSuccess = UpdateWindow(WindowHWND);

	//char* OutBuffer		=	new char[1024*1024];
	
	//for ( size_t N = 0; N < 1024*1024; N++ )
	//{
	//	if ( N % 2 == 0 )
	//	{
	//		OutBuffer[N] = 'L';
	//	}
	//	else
	//	{
	//		OutBuffer[N] = 'O';
	//	}
	//}

	//SyncWriteFile("C:\\MaxsiRXPTest.txt",OutBuffer,1024*1024,0);

	return bSuccess;
}

/****************************************************************************

    FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
    PURPOSE:  Processes messages

****************************************************************************/

LRESULT APIENTRY MaxsiRXP::WindowProc(
    HWND hWnd, 
    UINT message, 
    WPARAM wParam, 
    LPARAM lParam)
{
	return BaseClass::WindowProc(hWnd,message,wParam,lParam);
}


DWORD MaxsiRXP::OnThreadRegistered(HWND hWnd)
{
	return BaseClass::OnThreadRegistered(hWnd);
}